# -*- coding: utf-8 -*-

from wxgame import db
from . import ts_func


class Room(db.Model):
    __tablename__ = 'room'

    id = db.Column('id', db.String(32), primary_key=True)
    name = db.Column(db.String(128))
    headimgurl = db.Column(db.String(256))
    owner = db.Column(db.String(32))
    # robots_config = Column(String)
    # 0: offline 1: online 2: effective
    status = db.Column(db.Integer, nullable=False, default=1)
    create_time = db.Column(db.Integer, nullable=False, default=ts_func)

    robots = db.relationship(
        'RoomRobot',
        backref=db.backref('room', lazy='joined'),
        lazy='dynamic'
    )

    players = db.relationship(
        'RoomPlayer',
        backref=db.backref('room', lazy='joined'),
        lazy='dynamic'
    )

    gamesets = db.relationship(
        'Gameset',
        backref='room',
        lazy='dynamic'
    )

    transactions = db.relationship(
        'Transaction',
        backref='room',
        lazy='dynamic'
    )

    setting = db.relationship(
        'Setting',
        backref='target',
        uselist=False
    )

    def __repr__(self):
        return '<Room: id={}, name={}>'.format(self.id, self.name)


class RoomRobot(db.Model):
    __tablename__ = 'room_robot'

    id = db.Column(db.Integer, primary_key=True)
    room_id = db.Column(db.String(32), db.ForeignKey('room.id'), nullable=False)
    robot_wxid = db.Column(db.String(32), db.ForeignKey('robot.wxid'), nullable=False)
    robot_role = db.Column(db.Integer)
    status = db.Column(db.Integer, nullable=False, default=1)
    create_time = db.Column(db.Integer, nullable=False, default=ts_func)

    def __repr__(self):
        return '<RoomRobot: id={}, room_id={}, robot_wxid={}>'.format(self.id, self.room_id, self.robot_wxid)


class RoomPlayer(db.Model):
    __tablename__ = 'room_player'

    id = db.Column(db.Integer, primary_key=True)
    # 0: quited; 1: inactive; 2: activate(already joined)
    status = db.Column(db.Integer, nullable=False, default=1)
    create_time = db.Column(db.Integer, nullable=False, default=ts_func)

    room_id = db.Column(db.String(32), db.ForeignKey('room.id'), nullable=True)
    player_wxid = db.Column(db.String(32), db.ForeignKey('player.wxid'), nullable=True)
    inviter_wxid = db.Column(db.String(32), db.ForeignKey('player.wxid'), nullable=True)
    # agent_wxid = db.Column(db.String(32), db.ForeignKey('player.wxid'), nullable=True)
    # is_agent = db.Column(db.Boolean, nullable=False, default=False)
